<template>
  <div class="orrBusinessDurationEfficiency-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="门店编号" prop="storeNumber">
          <el-input
            placeholder="请输入门店编号"
            v-model="searchForm.storeNumber"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="门店名称" prop="storeName">
          <el-input
            placeholder="请输入门店名称"
            v-model="searchForm.storeName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-input
            placeholder="请输入日期"
            v-model="searchForm.date"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="营业时段" prop="periodTime">
          <el-input
            placeholder="请输入营业时段"
            v-model="searchForm.periodTime"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="出杯数量" prop="count">
          <el-input
            placeholder="请输入出杯数量"
            v-model="searchForm.count"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="CPU时间" prop="aveCpuTime">
          <el-input
            placeholder="请输入CPU时间"
            v-model="searchForm.aveCpuTime"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="物料准备时间" prop="aveMaterialTime">
          <el-input
            placeholder="请输入物料准备时间"
            v-model="searchForm.aveMaterialTime"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="操作时间（秒）" prop="aveOperateTime">
          <el-input
            placeholder="请输入操作时间（秒） "
            v-model="searchForm.aveOperateTime"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="操作占比" prop="aveOperateProportion">
          <el-input
            placeholder="请输入操作占比"
            v-model="searchForm.aveOperateProportion"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="物料占比" prop="aveMaterialProportion">
          <el-input
            placeholder="请输入物料占比"
            v-model="searchForm.aveMaterialProportion"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="操作占比字符串" prop="aveOperateProportions">
          <el-input
            placeholder="请输入操作占比字符串"
            v-model="searchForm.aveOperateProportions"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="物料占比字符串" prop="averageMaterialProportions">
          <el-input
            placeholder="请输入物料占比字符串"
            v-model="searchForm.averageMaterialProportions"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="btns-fun-field">
        <!-- v-if="userPermission.addUserPerm" -->
        <el-button
          type="primary"
          @click.prevent="showAddEditDialog"
          :icon="Plus"
        >
          新增营业时段效率汇总表
        </el-button>
      </div>
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="id" label="唯一标识" width="190" />
          <el-table-column prop="storeNumber" label="门店编号" width="190" />
          <el-table-column prop="storeName" label="门店名称" width="190" />
          <el-table-column prop="date" label="日期" width="190" />
          <el-table-column prop="periodTime" label="营业时段" width="190" />
          <el-table-column prop="count" label="出杯数量" width="190" />
          <el-table-column prop="aveCpuTime" label="CPU时间" width="190" />
          <el-table-column
            prop="aveMaterialTime"
            label="物料准备时间"
            width="190"
          />
          <el-table-column prop="aveOperateTime" label="操作时间（秒）"190" />
          <el-table-column
            prop="aveOperateProportion"
            label="操作占比"
            width="190"
          />
          <el-table-column
            prop="aveMaterialProportion"
            label="物料占比"
            width="190"
          />
          <el-table-column
            prop="aveOperateProportions"
            label="操作占比字符串"
            width="190"
          />
          <el-table-column
            prop="averageMaterialProportions"
            label="物料占比字符串"
            width="190"
          />
          <el-table-column prop="createTime" label="创建时间" width="190" />
          <el-table-column prop="updateTime" label="更新时间" width="190" />
          <el-table-column prop="createBy" label="创建人" width="190" />
          <el-table-column prop="updateBy" label="更新人" width="190" />
          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->
              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <OrrBusinessDurationEfficiencyAdd
      v-model="addUpdateRef"
    ></OrrBusinessDurationEfficiencyAdd>
    <OrrBusinessDurationEfficiencyRemove
      v-model="removeRef"
    ></OrrBusinessDurationEfficiencyRemove>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getOrrBusinessDurationEfficiencyPagination,
  getOrrBusinessDurationEfficiencyAllList,
} from '@/api/orr-business-duration-efficiency.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import OrrBusinessDurationEfficiencyRemove from './OrrBusinessDurationEfficiencyRemove.vue'
import OrrBusinessDurationEfficiencyAdd from './OrrBusinessDurationEfficiencyAdd.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'

const titleName = `营业时段效率汇总表`
// const userPermission = useUserPermission();

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `营业时段效率汇总表`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

// const requestCanUseCode = () => {
// 	getCanUseCode({ type: 8 })
// 			.then((res: any) => {
// 				console.log('getCanUseCode: ', res)
// 				addUpdateRef.form.number = res
// 			})
// 			.catch((err) => {
// 				ElMessage.error(err.msg)
// 			})
// }

const showAddEditDialog = (item: any, type: number) => {
  //新增
  if (item.target) {
    let form = {
      id: null,
      storeNumber: '',
      storeName: '',
      date: '',
      periodTime: '',
      count: '',
      aveCpuTime: '',
      aveMaterialTime: '',
      aveOperateTime: '',
      aveOperateProportion: '',
      aveMaterialProportion: '',
      aveOperateProportions: '',
      averageMaterialProportions: '',
      disabled: false,
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    // requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    storeNumber: item.storeNumber,
    storeName: item.storeName,
    date: item.date,
    periodTime: item.periodTime,
    count: item.count,
    aveCpuTime: item.aveCpuTime,
    aveMaterialTime: item.aveMaterialTime,
    aveOperateTime: item.aveOperateTime,
    aveOperateProportion: item.aveOperateProportion,
    aveMaterialProportion: item.aveMaterialProportion,
    aveOperateProportions: item.aveOperateProportions,
    averageMaterialProportions: item.averageMaterialProportions,
    disabled: false,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: `营业时段效率汇总表`,
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  storeNumber: '',
  storeName: '',
  date: '',
  periodTime: '',
  count: '',
  aveCpuTime: '',
  aveMaterialTime: '',
  aveOperateTime: '',
  aveOperateProportion: '',
  aveMaterialProportion: '',
  aveOperateProportions: '',
  averageMaterialProportions: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 清空搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let storeNumber = searchForm.value.storeNumber
  if (storeNumber) {
    searchCondition.storeNumber = storeNumber
  }
  let storeName = searchForm.value.storeName
  if (storeName) {
    searchCondition.storeName = storeName
  }
  let date = searchForm.value.date
  if (date) {
    searchCondition.date = date
  }
  let periodTime = searchForm.value.periodTime
  if (periodTime) {
    searchCondition.periodTime = periodTime
  }
  let count = searchForm.value.count
  if (count) {
    searchCondition.count = count
  }
  let aveCpuTime = searchForm.value.aveCpuTime
  if (aveCpuTime) {
    searchCondition.aveCpuTime = aveCpuTime
  }
  let aveMaterialTime = searchForm.value.aveMaterialTime
  if (aveMaterialTime) {
    searchCondition.aveMaterialTime = aveMaterialTime
  }
  let aveOperateTime = searchForm.value.aveOperateTime
  if (aveOperateTime) {
    searchCondition.aveOperateTime = aveOperateTime
  }
  let aveOperateProportion = searchForm.value.aveOperateProportion
  if (aveOperateProportion) {
    searchCondition.aveOperateProportion = aveOperateProportion
  }
  let aveMaterialProportion = searchForm.value.aveMaterialProportion
  if (aveMaterialProportion) {
    searchCondition.aveMaterialProportion = aveMaterialProportion
  }
  let aveOperateProportions = searchForm.value.aveOperateProportions
  if (aveOperateProportions) {
    searchCondition.aveOperateProportions = aveOperateProportions
  }
  let averageMaterialProportions = searchForm.value.averageMaterialProportions
  if (averageMaterialProportions) {
    searchCondition.averageMaterialProportions = averageMaterialProportions
  }

  tableLoading.value = true
  getOrrBusinessDurationEfficiencyPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})
</script>
<style lang="scss" scoped>
.orrBusinessDurationEfficiency-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.orrBusinessDurationEfficiency-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
